﻿// -----------------------------------------------------------------------
// <copyright company="www.3WayWebdesign.de">
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
// 
// <author>Michael Morbach</author>
// <email>Michael@3waywebdesign.de</email>
// <web>http://www.3waywebdesign.de</web>
// ----------------------------------------------------------------------

using System;
using System.Windows;
using System.Windows.Controls;
using NLog;
using Tww.Wpf.Controls.Controls.Flyouts;
using Tww.Wpf.Controls.Controls.Shared;
using Tww.Wpf.Controls.Controls.Shared.Enums;
using Tww.Wpf.Controls.Controls.Shared.Helper;
using Tww.Wpf.Controls.Controls.Shared.Interfaces;

namespace Tww.Wpf.Controls.Controls.Menus.ExpandMenu
{

    #region Imports

    #endregion

    [TemplatePart(Name = "PART_Header", Type = typeof (ContentPresenter))]
    public class ExpandMenu : TreeView, ISizeableElement
    {
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

        #region  - Felder (öffentlich) -

        public static readonly DependencyProperty ElementSizeProperty =
            DependencyProperty.Register("ElementSize", typeof (ElementSize), typeof (ExpandMenu),
                new FrameworkPropertyMetadata(ElementSize.Maximized,
                    FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, ExpandMenuElementSizePropertyChangedCallback));

        public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof (string),
            typeof (ExpandMenu), new PropertyMetadata(default(string)));

        public static readonly DependencyProperty HeaderTemplateProperty = DependencyProperty.Register(
            "HeaderTemplate", typeof (DataTemplate), typeof (ExpandMenu));

        #endregion

        #region  - Konstruktoren -

        static ExpandMenu()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof (ExpandMenu),
                new FrameworkPropertyMetadata(typeof (ExpandMenu)));
        }

        #endregion

        #region  - Indexer und Eigenschaften (öffentlich) -

        public DataTemplate HeaderTemplate
        {
            get { return (DataTemplate) GetValue(HeaderTemplateProperty); }
            set { SetValue(HeaderTemplateProperty, value); }
        }

        public string Header
        {
            get { return (string) GetValue(HeaderProperty); }
            set { SetValue(HeaderProperty, value); }
        }

        #endregion

        #region  - ISizeableElement Members -

        /// <summary>
        ///     Gets or sets the state of the flyout.
        /// </summary>
        /// <value>
        ///     The state of the flyout.
        /// </value>
        public ElementSize ElementSize
        {
            get { return (ElementSize) GetValue(ElementSizeProperty); }
            set { SetValue(ElementSizeProperty, value); }
        }

        #endregion

        #region  - Methoden (öffentlich) -

        public void SwitchElementSize(ElementSize newSize)
        {
            if (newSize != ElementSize)
            {
                var parentFlyout = this.TryFindParent<Flyout>();

                if (parentFlyout != null)
                {
                    switch (newSize)
                    {
                        case ElementSize.Maximized:
                            parentFlyout.MaximizeFlyout();
                            break;
                        case ElementSize.Minimized:
                            parentFlyout.MinimizeFlyout();
                            break;
                        case ElementSize.Collapsed:
                            parentFlyout.CloseFlyout();
                            break;
                        default:
                            throw new ArgumentOutOfRangeException("newSize");
                    }
                }
                else
                {
                    Logger.Warn("ParenFlyout is not existing. Can't change Size.");
                }
            }
        }

        #endregion

        #region  - Methoden (privat) -

        private static void ExpandMenuElementSizePropertyChangedCallback(DependencyObject dependencyObject,
            DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            var menu = (ExpandMenu) dependencyObject;
            var oldState = (ElementSize) dependencyPropertyChangedEventArgs.OldValue;
            var newState = (ElementSize) dependencyPropertyChangedEventArgs.NewValue;

            Logger.Trace("Callback change size from currentSize={0} to newSize={1}", oldState, newState);
            if (oldState != newState)
            {
                switch (newState)
                {
                    case ElementSize.Maximized:
                        VisualStateManager.GoToState(menu, VisualStates.StateMaximized, true);
                        break;
                    case ElementSize.Minimized:
                        VisualStateManager.GoToState(menu, VisualStates.StateMinimized, true);
                        break;
                    default:
                    case ElementSize.Collapsed:
                        VisualStateManager.GoToState(menu, VisualStates.StateCollapsed, true);
                        break;
                }
            }
        }

        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            return (item is ExpandMenuItem);
        }

        protected override DependencyObject GetContainerForItemOverride()
        {
            return new ExpandMenuItem();
        }

        #endregion
    }
}